package com.jdls.tanhua.web;

import com.baomidou.mybatisplus.extension.api.R;
import com.jdls.tanhua.commons.utils.JwtUtils;
import com.jdls.tanhua.interceptor.UserHolder;
import com.jdls.tanhua.pojo.UserInfo;
import com.jdls.tanhua.server.UserInfoServer;
import com.jdls.tanhua.server.UserServer;
import com.jdls.tanhua.template.OssTemplate;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import jdk.nashorn.internal.parser.Token;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * @author 嘉德罗斯
 * @date 2021/10/22
 */
@RequestMapping("/user")
@RestController
public class LoginWeb {

    @Autowired
    private UserServer userServer;

    @Autowired
    private OssTemplate ossTemplate;

    @Autowired
    private UserInfoServer userInfoServer;


    @PostMapping("/login")
    public void login(@RequestBody Map map){
        //接收到电话
        String phone = (String) map.get("phone");
        //调用Userver的方法
        userServer.sendMsg(phone);
    }


    @PostMapping("/loginVerification")
    //验证码校验
    public ResponseEntity loginVerification(@RequestBody Map map){
        String phone = (String) map.get("phone");
        String code = (String) map.get("verificationCode");
        Map retMap = userServer.loginVerification(phone,code);
        return ResponseEntity.ok(retMap);
    }

    @PostMapping("/loginReginfo")
    //新用户完善资料
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo,
                             @RequestHeader("Authorization") String token){

        userInfo.setId(UserHolder.getUserId());
        userInfoServer.save(userInfo);
        return ResponseEntity.ok(null);
    }

    @PostMapping("loginReginfo/head")
    public ResponseEntity img(@RequestHeader("Authorization") String token, @RequestBody MultipartFile headPhoto) throws IOException {
        Claims body = JwtUtils.getBody(token);
        Integer id = (Integer) body.get("id");
        userInfoServer.update(id,headPhoto);
        return ResponseEntity.ok(null);
    }
}
